<template>
  <div class="genderPop">
    <!-- 选择器 -->
    <van-picker
      title="标题"
      show-toolbar
      :default-index="gender"
      :columns="columns"
      @confirm="onConfirm"
      @cancel="$emit('close')"
    />
  </div>
</template>

<script>
// 编辑用户个人资料api
import { edituser } from '@/assets/api/user'
export default {
  name: 'genderPop',
  props: {
    gender: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      columns: ['男', '女']
    }
  },
  methods: {
    async onConfirm(value, index) {
      this.$toast.loading({
        message: '保存中',
        forbidClick: true,
        // 持续展示
        duration: 0
      })
      if (index === this.gender) {
        this.$toast('更新成功')
        return this.$emit('close')
      }
      try {
        // 发起请求
        var { data: res } = await edituser({
          gender: index
        })
      } catch {
        // 处理请求错误
        return this.$toast('更改失败，请取消在试')
      }
      // 告知父组件更改完毕
      this.$toast('更新成功')
      this.$emit('editOk', index)
    }
  }
}
</script>

<style lang="less" scoped>
.genderPop {
  /deep/ .van-nav-bar {
    background-color: #fff;
    .van-nav-bar__title {
      color: #000 !important;
    }
  }
}
</style>
